Newer
Older
taehui / taehui-fe / src / app / [language] / commentary / query / usePutCommentary.ts
@Taehui Taehui on 6 Apr 1 KB 2024-04-07 오전 8:25
import { isClientFault, wwwAPI } from "@/utilities/wwwAPI";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { useTranslations } from "next-intl";
import { toast } from "react-toastify";
import { getMillis } from "taehui-ts/date";

export default function usePutCommentary() {
  const queryClient = useQueryClient();

  const t = useTranslations();

  return useMutation({
    mutationFn: async ({
      commentaryID,
      avatarCipher,
      text,
    }: {
      commentaryID: number;
      avatarCipher: string;
      text: string;
    }) => {
      await wwwAPI.put(
        "/commentary",
        {
          commentaryID,
          avatarCipher,
          text,
        },
        {
          headers: {
            millis: getMillis(),
          },
        },
      );
    },
    onSuccess: async () => {
      await queryClient.invalidateQueries({ queryKey: ["commentary"] });
    },
    onError: (e) => {
      if (isClientFault(e)) {
        toast.error(t("failedValidateCipher"));
      }
    },
  });
}